package savet.gui.tabelas.salario.renderer;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import sun.swing.DefaultLookup;

/**
 *
 * @author Bsi 2007
 */
public class AtributoSalarioCellRenderer implements TableCellRenderer {

    private JLabel component;
    
    private Color unselectedForeground;
    private Color unselectedBackground;

    public AtributoSalarioCellRenderer() {
        component = new JLabel();
        component.setOpaque(true);
    }

    public void setForeground(Color c) {
        component.setForeground(c);
        unselectedForeground = c;
    }

    public void setBackground(Color c) {
        component.setBackground(c);
        unselectedBackground = c;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        Color fg = null;
        Color bg = null;

        JTable.DropLocation dropLocation = table.getDropLocation();
        if (dropLocation != null
                && !dropLocation.isInsertRow()
                && !dropLocation.isInsertColumn()
                && dropLocation.getRow() == row
                && dropLocation.getColumn() == column) {

            fg = DefaultLookup.getColor(component, component.getUI(), "Table.dropCellForeground");
            bg = DefaultLookup.getColor(component, component.getUI(), "Table.dropCellBackground");

            isSelected = true;
        }

        if (isSelected) {
            setForeground(fg == null ? table.getSelectionForeground()
                    : fg);
            component.setBackground(bg == null ? table.getSelectionBackground()
                    : bg);
        } else {
            Color background = unselectedBackground != null
                    ? unselectedBackground
                    : table.getBackground();
            if (background == null || background instanceof javax.swing.plaf.UIResource) {
                Color alternateColor = DefaultLookup.getColor(component, component.getUI(), "Table.alternateRowColor");
                if (alternateColor != null && row % 2 == 0) {
                    background = alternateColor;
                }
            }
            component.setForeground(unselectedForeground != null
                    ? unselectedForeground
                    : table.getForeground());
            component.setBackground(background);
        }

        component.setFont(table.getFont());

        if (hasFocus) {
            Border border = null;
            if (isSelected) {
                border = DefaultLookup.getBorder(component, component.getUI(), "Table.focusSelectedCellHighlightBorder");
            }
            if (border == null) {
                border = UIManager.getBorder("TableHeader.cellBorder");
            }
            component.setBorder(border);

            if (!isSelected && table.isCellEditable(row, column)) {
                Color col;
                col = DefaultLookup.getColor(component, component.getUI(), "Table.focusCellForeground");
                if (col != null) {
                    component.setForeground(col);
                }
                col = DefaultLookup.getColor(component, component.getUI(), "Table.focusCellBackground");
                if (col != null) {
                    component.setBackground(col);
                }
            }
        } else {
            component.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        }

        return component;
    }
}
